# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 16:25:22 2021

@author: zyx
"""
import cx_Oracle as cx
import datetime

from parameter.parameter import Parameter

# oracle_account = 'PASSWD123456789/123456789@10.254.4.121/orclcauc'  # 樊老师服务器，旧


# oracle_account = 'mfdyprice/mfdyprice@10.5.25.248:1521/ORCLPDB'  # 鲁老师服务器


# oracle_account = 'dypricemf/dypricemf1234@10.5.25.248:1521/ORCLPDB'  # 樊老师服务器，新


oracle_account = 'dypricemf/dypricemf988198@10.254.0.32:1521/orclpbd'


def get_data_from_database(flight:str):
    con = cx.connect(oracle_account)  # 樊老师服务器，旧
    cursor = con.cursor()

    flight=flight.split()[0]
    # flight = "8501"

    get_flight = "select flightdate,dow,ITTL360,ITTL180,ITTL90,ITTL55,ITTL45,ITTL35,ITTL28,ITTL25,ITTL22," \
                 "ITTL19,ITTL17,ITTL15,ITTL13,ITTL11,ITTL9,ITTL8,ITTL7,ITTL6,ITTL5,ITTL4,ITTL3," \
                 "ITTL2,ITTL1,ITTL0,ITTLn1 from dypricemf.FLIGHTSTAT t " \
                 "where carrier = 'MF' and flightno = '{}'" \
        .format(flight)
    cursor.execute(get_flight)
    desc = [d[0] for d in cursor.description]
    flight_data = [dict(zip(desc, line)) for line in cursor]
    cursor.close()
    con.close()
    return flight_data


def classify_train_and_test(data_list):
    """
        将数据分为训练集和测试集
    :param data_list: 总的数据，由多个字典组成的list，每个字典存储一条数据
    :return: 返回训练集和测试集，数据格式不变
    """
    train_data = []
    test_data = []
    for row in data_list:
        if row['FLIGHTDATE'] < Parameter.DATETIME_TRAIN_TEST.value:
            train_data.append(row)
        else:
            test_data.append(row)
    return train_data, test_data


def sava_data_to_database(center, result):
    """
        将数据保存到数据库
    :param center: 聚类中心
    :param result: 预测结果
    :return:
    """
    con = cx.connect(oracle_account)
    cursor = con.cursor()

    cursor.close()
    con.close()


if __name__ == '__main__':
    flight_data = get_data_from_database('8325')
    print(flight_data)
    train_data, test_data = classify_train_and_test(flight_data)
    print(1)
    # sava_data_to_database(1, 2)
